<?php
if(defined("IZ_ACCNAME")) {
function izsql() {
    global $dbhost;
    global $dbuser;
    global $dbpass;
    global $dbdatabase;
    global $sqlver;
    if(IZ_CALMODE == 'mysql') {
      if($_SESSION['MYSQLi']) {
        global $dblink;
        $dblink = mysqli_connect($dbhost,$dbuser,$dbpass,$dbdatabase);
        if(mysqli_connect_errno()) {
           printf("Can't connect to MySQL Server. Errorcode: %s\n", mysqli_connect_error());
           die();
        }
      } else {
        $dblink = mysql_connect($dbhost,$dbuser,$dbpass);
        mysql_select_db($dbdatabase,$dblink) or die('&#35;44&#58; '.ERR_DBCONNECTFAIL.'&#58; '.mysql_error());
        if(version_compare((string)$sqlver,"4.1",">=")) {
          @mysql_query("SET NAMES 'utf8'");
        }
      }
    } elseif(IZ_CALMODE == 'postgre') {
      return pg_connect("host=$dbhost dbname=$dbdatabase user=$dbuser password=$dbpass");
    } elseif(IZ_CALMODE == 'msq') {
      $dblink = mssql_connect($dbhost, $dbuser, $dbpass);
      mssql_select_db($dbdatabase, $dblink) or die('&#35;44&#58; '.ERR_DBCONNECTFAIL.'&#58; '.mssql_get_last_message());
    }
}
function clean($par) {
         // converts input to sanitised HTML
         $entities = get_html_translation_table(HTML_SPECIALCHARS);
         $extra = array("\r\n" => '<br />',
                       "\n\r" => '<br />',
                       "\n" => '<br />',
                       "\r" => '<br />',
                       "__NL__" => '<br />',
                       "__SPC__" => '<br />',
                       "'" => '&#39;',
                       '"' => '&#34;'
                  );
         $trans = array_merge($entities,$extra);
         $cleaned = strtr($par,$trans);
         return $cleaned;
}
function get_day($epochsecs) {
         global $havealldayevents;
         global $havestandardevents;
         list($day, $month, $year) = explode(" ",date("j n Y",$epochsecs));
         $return .= '<h1>'.iz_longdate($epochsecs).'</h1>';
         $return .= get_events('allday',$epochsecs,'none');
         if(($_SESSION['IZ_OPT'] & OPT_ENDDATE) != 0) {
           for($hour=0;$hour<24;$hour++) {
             $houroffset = 3600*$hour;
             $standardevents .= get_events('daynormal',$epochsecs,$houroffset);
           }
         }
         if(!$havealldayevents && !$havestandardevents) {
           $return .= '<p>'.STR_NOEVENTS.'</p>';
         } else {
           if($havestandardevents) {
             $return .= '<h2>'.STR_EVENTS.'</h2>'.$standardevents;
           }
         }
         return $return;
}
function get_month($rmonth = 0, $ryear = 0) {
        global $itemno;
        global $weekdays;
        global $monthnames;
        global $curYear;
        global $curMonth;
        if($ryear == 0) {
          $year = date('Y',mktime(0,0,0,1,1,$curYear));
        } else {
          $year = date('Y',mktime(0,0,0,1,1,$ryear));
        }
        if($rmonth == 0) {
          $month = date('n',mktime(0,0,0,$curMonth,1,$year));
        } else {
          $month = date('n',mktime(0,0,0,$rmonth,1,$year));
        }
        $firstdayofmonth = mktime(0,0,0,$month,1,$year);
        $lastdayofmonth = mktime(0,0,0,$month+1,1,$year) - 86400;
        $numdays = date('t',$firstdayofmonth);
        $monthinfo = getdate($firstdayofmonth);
        $lastdayinfo = getdate($lastdayofmonth);
        if($_SESSION['IZ_FIRSTDAY'] == 0) {
          $wday = $monthinfo['wday'];
          $lastdaymo = $lastdayinfo['wday'];
        } else {
          $lastdaymo = $lastdayinfo['wday']-1;
          if($monthinfo['wday'] == 0) {
            $wday = 6;
          } else {
            $wday = $monthinfo['wday']-1;
          }
        }
        $monthname = $monthnames[date('n',$firstdayofmonth)];
        $itemno = 1;
        $return .= '<h1>'.$monthname.' '.$year.'</h1>';
        for($loopday = 1; $loopday<=$numdays; $loopday++) {
          $return .= get_events('month', $loopday, $firstdayofmonth);
        }
        return $return;
}
function getlink($type,$secs) {
     global $monthnames;
     global $weekdays;
     list($day, $month, $year) = explode(" ",date("j n Y",$secs));
//echo 'd:',$day,'m:',$month,'y:',$year;
     switch($type) {
         case 'month':
             $period = mktime(0,0,0,$month,$day,$year);
             $viewtype = 'month';
             $accesskey = ' accesskey="2"';
             $ltitle = $monthnames[$month].' '.$year;
             break;
         case 'monthnext':
             if($month == '12') {
               $period = mktime(0,0,0,1,$day,$year+1);
             } else {
               $period = mktime(0,0,0,$month+1,$day,$year);
             }
             $viewtype = 'month';
             $where = 'next';
             $accesskey = ' accesskey="3"';
             $ltitle = $monthnames[date('n',$period)].' >';
             break;
         case 'monthprev':
             if($month == 1) {
               $period = mktime(0,0,0,12,$day,$year-1);
             } else {
               $period = mktime(0,0,0,$month-1,$day,$year);
             }
             $viewtype = 'month';
             $where = 'prev';
             $accesskey = ' accesskey="1"';
             $ltitle = '< '.$monthnames[date('n',$period)];
             break;
         case 'day':
             $period = mktime(0,0,0,$month,$day,$year);
             $viewtype = 'day';
             $ltitle = iz_longdate($period);
             break;
         case 'daynext':
             if($day == date('t',mktime(0,0,0,$month,$day,$year))) {
               if($month == 12) {
                 $period = mktime(0,0,0,1,1,$year+1);
               } else {
                 $period = mktime(0,0,0,$month+1,1,$year);
               }
             } else {
               $period = mktime(0,0,0,$month,$day+1,$year);
             }
             $viewtype = 'day';
             $where = 'next';
             $accesskey = ' accesskey="3"';
             $wday = date('w',$period);
             if($wday == 0) { //sunday
               $ltitle = $weekdays[6].' '.date('j',$period).' >';
             } else {
               $ltitle = $weekdays[$wday-1].' '.date('j',$period).' >';
             }
             break;
         case 'dayprev':
             if($day == 1) {
               if($month == 1) {
                 $period = mktime(0,0,0,12,31,$year-1);
               } else {
                 $period = mktime(0,0,0,$month,0,$year);
               }
             } else {
               $period = mktime(0,0,0,$month,$day-1,$year);
             }
             $viewtype = 'day';
             $accesskey = ' accesskey="1"';
             $wday = date('w',$period);
             if($wday == 0) { //sunday
               $ltitle = '< '.$weekdays[6].' '.date('j',$period);
             } else {
               $ltitle = '< '.$weekdays[$wday-1].' '.date('j',$period);
             }
             $where = 'prev';
             break;
     }
     $day = date('j',$period);
     $month = date('n',$period);
     $year = date('Y',$period);
     if($_SESSION['IZ_REWRITE'] == 'apache') {
       echo '<a class="',$where,'link"',$accesskey,' href="',$_SESSION['IZ_CALURL'],'m/',$viewtype,'/',$year,'/',str_pad($month,2,'0',STR_PAD_LEFT),'/',str_pad($day,2,'0',STR_PAD_LEFT),'/">',$ltitle,'</a>';
     } else {
       echo '<a class="',$where,'link"',$accesskey,' href="',$_SESSION['IZ_CALURL'],'m.php?view=',$viewtype,'&amp;d=',$day,'&amp;m=',$month,'&amp;y=',$year,'">',$ltitle,'</a>';
     }
}
function get_events($period, $par1, $par2) {
         global $numqs;
         global $curDay;
         global $curMonth;
         global $monthnames;
         global $curYear;
         global $itemno;
         global $havealldayevents;
         global $havestandardevents;
         izsql();
         if($period == 'month') {
            $periodstart = mktime(0,0,0,date('n',$par2),$par1,date('Y',$par2));
            $periodend = mktime(23,59,59,date('n',$par2),$par1,date('Y',$par2));        
            $fromday = date('j',$par2);
            $frommonth = date('n',$par2);
            $fromyear = date('Y',$par2);
            $events = array();
            $eventsql = "SELECT * FROM events WHERE (( eventstart BETWEEN '".$periodstart."' AND '".$periodend."' ) AND ((LOWER(author) = '".strtolower($_SESSION['uname'])."')".$_SESSION['publicstatus'].")) ORDER BY eventstart";
            if(IZ_CALMODE == 'mysql') {
              if($_SESSION['MYSQLi']) {
                global $dblink;
                if($eventqry = $dblink->query($eventsql)) {
                  while($sqlevent = $eventqry->fetch_object()) {
                    $events[] = new event($sqlevent);
                  }
                  $eventqry->close();
                }
              } else {
                $eventqry = mysql_query($eventsql);
                if(is_resource($eventqry)) {
                  while($sqlevent = mysql_fetch_object($eventqry)) {
                    $events[] = new event($sqlevent);
                  }
                }
              }
            } elseif(IZ_CALMODE == 'postgre') {
              $eventsql = "SELECT * FROM \"events\" WHERE (( \"eventstart\" BETWEEN '".$periodstart."' AND '".$periodend."' ) AND ((\"author\" = '".strtolower($_SESSION['uname'])."')".$_SESSION['publicstatus'].")) ORDER BY \"eventstart\"";
              $eventqry = pg_query($eventsql);
              if(is_resource($eventqry)) {
                while($sqlevent = pg_fetch_object($eventqry)) {
                  $events[] = new event($sqlevent);
                }
              }
            } elseif(IZ_CALMODE == 'msq') {
              $eventqry = mssql_query($eventsql);
              if(is_resource($eventqry)) {
                while($sqlevent = mssql_fetch_object($eventqry)) {
                  $events[] = new event($sqlevent);
                }
              }
            }
            $numqs++;
            $eventnum = count($events);
            if($eventnum > 0) {
                $return .= '<div class="itemview">
                            <h2><a href="'.$_SESSION['IZ_CALURL'].'m.php?view=day&amp;date='.date("Y-m-d",$periodstart).'">'.$par1.' '.$monthnames[$frommonth];
                if($periodstart == mktime(0,0,0,$curMonth,$curDay,$curYear)) {
                  $return .= ' &#40;'.STR_TODAY.'&#41;</a>';
                } else {
                  $return .= '</a>';
                }
                $return .= '</h2>'."\n";
                foreach($events as $event) {
                  $return .= '<a href="'.$_SESSION['IZ_CALURL'].'m.php?view=event&amp;event='.$event->id.'">'.date("H:i",$event->eventstart).' - '.$event->title.'</a><br />'.
                             '<em>'.$event->venue.'</em><br />';
                }
                $return .= '</div>'."\n";
            }
         } elseif($period == 'daynormal') {
            $daystart = mktime(0,0,0,date('n',$par1),date('j',$par1),date('Y',$par1));
            $periodstart = $daystart+$par2;
            $periodend = $periodstart + 3599;
            $fromday = date('j',$par1);
            $frommonth = date('n',$par1);
            $fromyear = date('Y',$par1);            
            $events = array();
 /*           if($_SESSION['cpanel'] == 1) {
              $eventsql = "SELECT * FROM events WHERE ( eventstart BETWEEN '".$periodstart."' AND '".$periodend."' ) AND allday != 1";
            } else {
              $eventsql = "SELECT * FROM events WHERE (( eventstart BETWEEN '".$periodstart."' AND '".$periodend."' ) AND allday != 1 AND ((LOWER(author) = '".strtolower($_SESSION['uname'])."')".$_SESSION['publicstatus']."))";
            } */
            $eventsql = "SELECT * FROM events WHERE (( eventstart BETWEEN '".$periodstart."' AND '".$periodend."' ) AND allday != 1 AND ((LOWER(author) = '".strtolower($_SESSION['uname'])."') OR status = 'Public'))";
            if(IZ_CALMODE == 'mysql') {
              if($_SESSION['MYSQLi']) {
                global $dblink;
                if($eventqry = $dblink->query($eventsql)) {
                  while($sqlevent = $eventqry->fetch_object()) {
                    $events[] = new event($sqlevent);
                  }
                  $eventqry->close();
                }
              } else {
                $eventqry = mysql_query($eventsql);
                if(is_resource($eventqry)) {
                  while($sqlevent = mysql_fetch_object($eventqry)) {
                    $events[] = new event($sqlevent);
                  }
                }
              }
            } elseif(IZ_CALMODE == 'postgre') {
              $eventsql = "SELECT * FROM \"events\" WHERE (( \"eventstart\" BETWEEN '".$periodstart."' AND '".$periodend."' ) AND \"allday\" != 1 AND ((\"author\" = '".strtolower($_SESSION['uname'])."') OR \"status\" = 'Public'))";
              $eventqry = pg_query($eventsql);
              if(is_resource($eventqry)) {
                while($sqlevent = pg_fetch_object($eventqry)) {
                  $events[] = new event($sqlevent);
                }
              }
            } elseif(IZ_CALMODE == 'msq') {
              $eventqry = mssql_query($eventsql);
              if(is_resource($eventqry)) {
                while($sqlevent = mssql_fetch_object($eventqry)) {
                  $events[] = new event($sqlevent);
                }              
              }
            }
            $numqs++;
            $eventnum = count($events);
            if($eventnum > 0) {
                foreach($events as $event) {
                  $return .= '<a href="'.$_SESSION['IZ_CALURL'].'m.php?view=event&amp;event='.$event->id.'">'.date("H:i",$event->eventstart).' - '.$event->title.'</a><br />';
                  if($event->venue != '') {
                    $return .= '<em>';
                      if($event->linkgoogle == 1) { 
                        $return .= '<a href="http://maps.google.co.uk/maps?q='.$event->venue.'" title="'.STR_LINKGOOGLE.'">'.$event->venue.'</a>';
                      } else {
                        $return .= $event->venue;
                      }                    
                    $return .= '</em><br />';
                  }
                }
                $havestandardevents = 1;
            }
          } elseif($period == 'allday') {
                $periodstart = mktime(0,0,0,date('n',$par1),date('j',$par1),date('Y',$par1));
                $periodend = $periodstart + 86399;
                $fromday = date('j',$par1);
                $frommonth = date('n',$par1);
                $fromyear = date('Y',$par1);
                $events = array();
                $eventsql = "SELECT * FROM events WHERE (( eventstart BETWEEN '".$periodstart."' AND '".$periodend."' ) AND allday = 1 AND ((LOWER(author) = '".strtolower($_SESSION['uname'])."') OR status = 'Public'))";
                if(IZ_CALMODE == 'mysql') {
                  if($_SESSION['MYSQLi']) {
                    global $dblink;
                    if($eventqry = $dblink->query($eventsql)) {
                      while($sqlevent = $eventqry->fetch_object()) {
                        $events[] = new event($sqlevent);
                      }
                      $eventqry->close();
                    }
                  } else {
                    $eventqry = mysql_query($eventsql);
                    if(is_resource($eventqry)) {
                      while($sqlevent = mysql_fetch_object($eventqry)) {
                        $events[] = new event($sqlevent);
                      }
                    }
                  }
                } elseif(IZ_CALMODE == 'postgre') {
                  $eventsql = "SELECT * FROM \"events\" WHERE (( \"eventstart\" BETWEEN '".$periodstart."' AND '".$periodend."' ) AND \"allday\" = 1 AND ((\"author\" = '".strtolower($_SESSION['uname'])."') OR \"status\" = 'Public'))";
                  $eventqry = pg_query($eventsql);
                  if(is_resource($eventqry)) {
                    while($sqlevent = pg_fetch_object($eventqry)) {
                      $events[] = new event($sqlevent);
                    }              
                  }
                } elseif(IZ_CALMODE == 'msq') {
                  $eventqry = mssql_query($eventsql);
                  if(is_resource($eventqry)) {
                    while($sqlevent = mssql_fetch_object($eventqry)) {
                      $events[] = new event($sqlevent);
                    }              
                  }
                }
                $numqs++;
                $eventnum = count($events);
                if($eventnum > 0) {
                  if(($_SESSION['IZ_OPT'] & OPT_ENDDATE) != 0) {
                    $return .= '<h2>'.STR_ALLDAY.'</h2>';
                  }
                  foreach($events as $event) {
                    $return .= '<a href="'.$_SESSION['IZ_CALURL'].'m.php?view=event&amp;event='.$event->id.'">'.$event->title.'</a><br />';
                    if($event->venue != '') {
                      $return .= '<em>';
                        if($event->linkgoogle == 1) { 
                          $return .= '<a href="http://maps.google.co.uk/maps?q='.$event->venue.'" title="'.STR_LINKGOOGLE.'">'.$event->venue.'</a>';
                        } else {
                          $return .= $event->venue;
                        }                    
                      $return .= '</em><br />';
                    }
                  }
                  $return .= '<br />';
                  $havealldayevents = 1;
                } else {
                    $return .= '<p>'.STR_NOALLDAY.'</p>';
                }
          }
          return $return;
}
function getdetails($id) {
          izsql();
          if(IZ_CALMODE == 'mysql') {
            if($_SESSION['MYSQLi']) {
              global $dblink;
              $safeid = $dblink->real_escape_string($id);
            } else {
              $safeid = mysql_real_escape_string($id);
            }
          } elseif(IZ_CALMODE == 'postgre') {
            $safeid = pg_escape_string($id);
          } elseif(IZ_CALMODE == 'msq') {
            $safeid = mssql_escape_string($id);
          }
          if(IZ_CALMODE == 'mysql') {
            if($_SESSION['MYSQLi']) {
              global $dblink;
              $eventsql = sprintf("SELECT * FROM events WHERE id = '%s'",$dblink->real_escape_string($id));
              if($eventqry = $dblink->query($eventsql)) {
                $sqlevent = $eventqry->fetch_object();
                $event = new event($sqlevent);
                $eventqry->close();
              }

            } else {
              $eventsql = sprintf("SELECT * FROM events WHERE id = '%s'",mysql_real_escape_string($id));
              $eventqry = mysql_query($eventsql);
              if(is_resource($eventqry)) {
                $sqlevent = mysql_fetch_object($eventqry);
                $event = new event($sqlevent);
              }            
            }
          } elseif(IZ_CALMODE == 'postgre') {
            $eventsql = sprintf("SELECT * FROM \"events\" WHERE \"id\" = '%s'",pg_escape_string($id));
            $eventqry = pg_query($eventsql);
            if(is_resource($eventqry)) {
              $sqlevent = pg_fetch_object($eventqry);
              $event = new event($sqlevent);
            }
          } elseif(IZ_CALMODE == 'msq') {
            $eventsql = sprintf("SELECT * FROM events WHERE id = '%s'",mssql_escape_string($id));
            $eventqry = mssql_query($eventsql);
            if(is_resource($eventqry)) {
              $sqlevent = mssql_fetch_object($eventqry);
              $event = new event($sqlevent);
            }
          }
          if(!$event) {
            $event = new event('','','','',ERR_EVNOTFOUND,'','','','','','','','','','','');
          }
          return $event;
}
function showdetails($event) {
            $return .= '<h1>'.$event->title.'</h1>
                        <strong>'.iz_longdate($event->eventstart).'</strong><br />';
            if($event->allday != 1) {
              $return .= date("H:i",$event->eventstart).' - '.date("H:i",$event->eventend).'<br />';
            }
            if($event->venue != '') {
              $return .= '<em>';
                if($event->linkgoogle == 1) {
                  $return .= '<a href="http://maps.google.co.uk/maps?q='.$event->venue.'" title="'.STR_LINKGOOGLE.'">'.$event->venue.'</a>';
                } else {
                  $return .= $event->venue;
                }                    
              $return .= '</em><br />';
            }
            if(strlen($event->description)>60) {
              $return .= substr($event->description,0,60)."...";
            } else {
              $return .= $event->description;
            }
            if(($event->cust1 != '') && ($_SESSION['cust1'] != 'none')){
              $return .= '<strong>'.$_SESSION['cust1'].':</strong> '.$event->cust1.'<br />';
            }
            if(($event->cust2 != '') && ($_SESSION['cust2'] != 'none')){
              $return .= '<strong>'.$_SESSION['cust2'].':</strong> '.$event->cust2.'<br />';
            }
            if(($event->cust3 != '') && ($_SESSION['cust3'] != 'none')) {
              $return .= '<strong>'.$_SESSION['cust3'].':</strong> '.$event->cust3.'<br />';
            }
            return $return;
}
function getnavmenu($daynum = '',$monthnum = '',$yearnum = '') {
    global $monthnames;
    global $curDay;
    global $curMonth;
    global $curYear;
    $view = 'day'; //could be changed later
    if(empty($daynum)) {
      $daynum = $curDay;
    }
    if(empty($monthnum)) {
      $monthnum = $curMonth;
    }
    if(empty($yearnum)) {
      $yearnum = $curYear;
    }
    echo '<form method="get" action="',$_SESSION['IZ_CALURL'],'m.php">
          <h1>',STR_GOTODAY,'</h1>';
    if(($view == 'day') || ($view == 'week')) {
      echo '<select size="1" name="d" id="day">';
      for ($ds=1; $ds<32; $ds++) {
        if($ds == $daynum) {
          echo '<option selected="selected">',$ds,'</option>';
        } else {
          echo '<option>',$ds,'</option>';
        }
      }
      echo '</select>';
    }
    if(($view == 'day') || ($view == 'month')) {
      echo '<select size="1" name="m" id="month">';
      foreach ($monthnames as $num => $mname) {
        if($num == $monthnum) {
          echo '<option selected="selected" value="',$num,'">',$mname,'</option>';
        } else {
          echo '<option value="',$num,'">',$mname,'</option>';
        }
      }
      echo '</select>';
    }
    echo '<select size="1" name="y" id="year">';
    for ($ys=2006; $ys<2040; $ys++) {
      if($ys == $yearnum) {
        echo '<option selected="selected">',$ys,'</option>';
      } else {
        echo '<option>',$ys,'</option>';
      }
    }
    echo '</select>
      <input type="hidden" name="view" value="',$view,'" />
          <input type="submit" value="',STR_GO,'" name="viewform" />
          </form>';
}
function get_categories() {
         izsql();
         $eventsql = "SELECT * FROM categories WHERE type = 'event' AND owner = '".strtolower($_SESSION['uname'])."'";
         $evsql = "SELECT * FROM categories WHERE type = 'event' AND (owner = 'everyone')";
         if(IZ_CALMODE == 'mysql') {
           if($_SESSION['MYSQLi']) {
             global $dblink;
             $eventqry = $dblink->query($eventsql);
             while($catobj = $eventqry->fetch_object()) {
               $evcats[$catobj->id] = $catobj->name;
               $numeventcats++;
             }
             $eventqry->close();
             if(strtolower($_SESSION['uname']) != 'everyone') {
               $eventqry = $dblink->query($evsql);
               while($catobj = $eventqry->fetch_object()) {
                 $evcats[$catobj->id] = $catobj->name;
                 $numeventcats++;
               }
               $eventqry->close();
             }
           } else {
             $eventqry = mysql_query($eventsql);
             while($catobj = mysql_fetch_object($eventqry)) {
               $evcats[$catobj->id] = $catobj->name;
               $numeventcats++;
             }
             if(strtolower($_SESSION['uname']) != 'everyone') {
               $eventqry = mysql_query($evsql);
               while($catobj = mysql_fetch_object($eventqry)) {
                 $evcats[$catobj->id] = $catobj->name;
                 $numeventcats++;
               }
             }
           }
         } elseif(IZ_CALMODE == 'postgre') {
           $eventsql = "SELECT * FROM \"categories\" WHERE \"type\" = 'event' AND \"owner\" = '".strtolower($_SESSION['uname'])."'";
           $evsql = "SELECT * FROM \"categories\" WHERE \"type\" = 'event' AND (\"owner\" = 'everyone')";
           $eventqry = pg_query($eventsql);
           while($catobj = pg_fetch_object($eventqry)) {
             $evcats[$catobj->id] = $catobj->name;
             $numeventcats++;
           }
           if(strtolower($_SESSION['uname']) != 'everyone') {
             $eventqry = pg_query($evsql);
             while($catobj = pg_fetch_object($eventqry)) {
               $evcats[$catobj->id] = $catobj->name;
               $numeventcats++;
             }
           }
         } elseif(IZ_CALMODE == 'msq') {
           $eventqry = mssql_query($eventsql);
           while($catobj = mssql_fetch_object($eventqry)) {
             $evcats[$catobj->id] = $catobj->name;
             $numeventcats++;
           }
           if(strtolower($_SESSION['uname']) != 'everyone') {
             $eventqry = mssql_query($evsql);
             while($catobj = mssql_fetch_object($eventqry)) {
               $evcats[$catobj->id] = $catobj->name;
               $numeventcats++;
             }
           }
         }
         $numqs++;
  // normally jslist method
         if($numeventcats > 0) {
           foreach($evcats as $cid => $cname) {
             $evcats2[] = $cid;
           }
             echo 'var cats = new Array("'.implode('","',$evcats2).'");'."\n";
           }
}
function iz_longdate($secs) {
    global $daynames;
    global $monthnames;
    $monthnum = date('n',$secs);
    $daylong = $daynames[date('w',$secs)];
    $monthlong = $monthnames[$monthnum];
        $trans = array("dd" => date('d',$secs), //xd
                  "d" => date('j',$secs),      //xj
                  "M" => $monthnum,            //xn
                  "MM" => date('m',$secs),     //xm
                  "yyyy" => date('Y',$secs),   //xY
                  "-S" => date('S',$secs),     //xS
                  "dddd" => $daylong,          //xl
                  "MMMM" => $monthlong,        //xF
                  "xp" => strtolower($monthlong),
                  "hh" => date('h',$secs),     //xh
                  "HH" => date('H',$secs),     //xH
                  "mm" => date('i',$secs),     //xi
                  "tt" => date('A',$secs)      //xA
                  );
         return strtr(IZ_LONGDATE,$trans);
}
function iz_longdatehours($secs) {
    global $daynames;
    global $monthnames;
    $monthnum = date('n',$secs);
    $daylong = $daynames[date('w',$secs)];
    $monthlong = $monthnames[$monthnum];
        $trans = array("dd" => date('d',$secs), //xd
                  "d" => date('j',$secs),      //xj
                  "M" => $monthnum,            //xn
                  "MM" => date('m',$secs),     //xm
                  "yyyy" => date('Y',$secs),   //xY
                  "-S" => date('S',$secs),     //xS
                  "dddd" => $daylong,          //xl
                  "MMMM" => $monthlong,        //xF
                  "xp" => strtolower($monthlong),
                  "hh" => date('h',$secs),     //xh
                  "HH" => date('H',$secs),     //xH
                  "mm" => date('i',$secs),     //xi
                  "tt" => date('A',$secs)      //xA
                  );
         return strtr(IZ_LONGDATEHOURS,$trans);
}
function init_cats() {
         izsql();
         $eventssql = "SELECT name,id FROM categories WHERE type = 'event' AND owner = '".strtolower($_SESSION['uname'])."'";
         if(IZ_CALMODE == 'mysql') {
           if($_SESSION['MYSQLi']) {
             global $dblink;
             $eventscats = $dblink->query($eventssql);
             while($catobj = $eventscats->fetch_object()) {
                 $eventscatids[] = $catobj->id;
                 $_SESSION['eventcats'][$catobj->id] = $catobj->name;
                 $_SESSION['eventcats'][$catobj->id]->status = 1;
                 $_SESSION['numeventcats']++;
                 $_SESSION['catidx'][strtolower($catobj->name)] = $catobj->id;
             }
             $eventscats->close();
           } else {
             $eventscats = mysql_query($eventssql);
             while($catobj = mysql_fetch_object($eventscats)) {
                 $eventscatids[] = $catobj->id;
                 $_SESSION['eventcats'][$catobj->id] = $catobj->name;
                 $_SESSION['eventcats'][$catobj->id]->status = 1;
                 $_SESSION['numeventcats']++;
                 $_SESSION['catidx'][strtolower($catobj->name)] = $catobj->id;
             }
           }
         } elseif(IZ_CALMODE == 'postgre') {
           $eventssql = "SELECT \"name\",\"id\" FROM \"categories\" WHERE \"type\" = 'event' AND \"owner\" = '".strtolower($_SESSION['uname'])."'";
           $eventscats = pg_query($eventssql);
           while($catobj = pg_fetch_object($eventscats)) {
               $eventscatids[] = $catobj->id;
                 $_SESSION['eventcats'][$catobj->id] = $catobj->name;
                 $_SESSION['eventcats'][$catobj->id]->status = 1;
               $_SESSION['numeventcats']++;
               $_SESSION['catidx'][strtolower($catobj->name)] = $catobj->id;
           }
         } elseif(IZ_CALMODE == 'msq') {
           $eventscats = mssql_query($eventssql);
           while($catobj = mssql_fetch_object($eventscats)) {
               $eventscatids[] = $catobj->id;
                 $_SESSION['eventcats'][$catobj->id] = $catobj->name;
                 $_SESSION['eventcats'][$catobj->id]->status = 1;
               $_SESSION['numeventcats']++;
               $_SESSION['catidx'][strtolower($catobj->name)] = $catobj->id;
           }
         }
         $o = 1;
         if(is_array($eventscatids)) {
           foreach($eventscatids as $id) {
               if($o == 1) {
                 $_SESSION['eventcats']['filters'] = " AND ( category = '".$id."'";
                 $o++;
               } else {
                 $_SESSION['eventcats']['filters'] .= " OR category = '".$id."'";
               }
           }
           $_SESSION['eventcats']['filters'] .= ' )';
         } else {
           $_SESSION['eventcats']['filters'] = "";
         }
}
/**
Calculates the column index of the specified day.
@param dayNum The actual number of the day (0=sunday - 6=saturday)
@param firstDay The starting position in the current view
*/
function getOffsetDisplayedIndex($dayNum,$firstDay='') {
    if($firstDay == '') {
      $first = $_SESSION['IZ_FIRSTDAY'];
    } else {
      $first = $firstDay;
    }
    $lower = (7 - $first) + $dayNum;
    if($lower < 7) {
        return $lower;
    } else {
        return $lower - 7;
    }
}
/**
Maps a column number to the array index in $daynames/$weekdays which holds the name of that day
Ie: Converts a column number to a day index
*/
function getLocalizedIndex($dayNum) {
    $lower = $_SESSION['IZ_FIRSTDAY'] +$dayNum;
    if($lower < 7) {
        return $lower;
    } else {
        return $lower - 7;
    }
}
function iz_error($txt) {
   echo '<div id="errorbox">'.$txt.'</div>';
}
function mssql_escape_string($string_to_escape) {
  $replaced_string = str_replace("'","''",$string_to_escape);
  return $replaced_string;
}
} else {
  // Directly accessed
  header('Location: ../');
} ?>